/**
 * @author Eduardo Leonne Souza de Oliveira
 * @version 1.0
 * Date: 2011-12-29
 * Last change: 2011-12-29 
 * 
 */

package com.leonne.harpacristabrasileira.dao;

import java.util.ArrayList;
import java.util.List;

import com.leonne.harpacristabrasileira.singleton.Connection;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

/**
 * Generic class to construct DAOs
 */

public class GenericDAO {

	private SQLiteDatabase database;
	private String tableName;
	private SQLiteStatement insertStmt;
	private String INSERT;
	private Context context;

	/**
	 * Default constructor
	 * 
	 * @param context
	 *            The application base context
	 * @param tableName
	 *            The name of the table that the DAO represents
	 */

	public GenericDAO(Context context, String tableName) {
		this.database = Connection.getConnection(context);
		this.tableName = tableName;
		this.context = context;
	}
	
	/**
	 * Simple get context function.
	 * @return The Activity context
	 */
	public Context getContext() {
		return this.context;
	}

	/**
	 * Pre-compile the insert query to make the insert more easy
	 */
	private void compileStatements() {
		this.insertStmt = this.database.compileStatement(INSERT);
	}

	/**
	 * Get the open database
	 * 
	 * @return A read/write database to manipulate the application data
	 */
	public SQLiteDatabase getDatabase() {
		return database;
	}

	/**
	 * Get the name of the table that the DAO represents
	 * 
	 * @return A String containing the name of the table that the DAO represents
	 */
	public String getTableName() {
		return tableName;
	}

	/**
	 * Set the name of the table that the DAO represents
	 * 
	 * @param tableName
	 *            A String containing the name of the table that the DAO
	 *            represents
	 */
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	/**
	 * Get the insert query for the table that the DAO represents
	 * 
	 * @return A String containing the insert query for the table that the DAO
	 *         represents
	 */
	public String getINSERT() {
		return INSERT;
	}

	/**
	 * Set the insert query for the table that the DAO represents
	 * 
	 * @param iNSERT
	 *            A String containing the insert query for the table that the
	 *            DAO represents.
	 *            <p>
	 *            Important: Query format
	 *            <p>
	 *            "INSERT INTO <table_name> (<column name1>, <column name2>, ...) VALUES (?, ?, ...)"
	 *            ;
	 */
	public void setINSERT(String iNSERT) {
		INSERT = iNSERT;
	}

	/**
	 * Delete all records in the table
	 */
	public void deleteAll() {
		this.database.delete(this.tableName, null, null);
	}
	
	public void execSql(String sql){
		this.database.execSQL(sql);
	}

}
